package com.aman.ui;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.aman.transformer.sample.R;

import java.util.Arrays;
import java.util.List;


public class ImageShowActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_show);

        final FrameLayout scalpeFrame = (FrameLayout) findViewById(R.id.scalpeFrame);
        final ViewPager pager = (ViewPager) findViewById(R.id.viewpager);
        final List<Integer> images = Arrays.asList(R.drawable.splash, R.drawable.test7,
                R.drawable.splash, R.drawable.test7,
                R.drawable.splash, R.drawable.test7);


        //
        scalpeFrame.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // dispatch the events to the ViewPager, to solve the problem that we can swipe only the middle view.
                return pager.dispatchTouchEvent(event);
            }
        });
        //pager.setPageTransformer(false, new ParallaxTransformer());
        pager.setAdapter(new SampleAdapter(this, images));
        pager.setOffscreenPageLimit(images.size());
        pager.setPageMargin(20);
        ViewGroup.MarginLayoutParams margin = (ViewGroup.MarginLayoutParams) pager.getLayoutParams();
        margin.rightMargin = 250;
        margin.leftMargin = 0;
        pager.setLayoutParams(margin);
        pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                if (position + 2 == images.size()) {
                    ViewGroup.MarginLayoutParams margin = (ViewGroup.MarginLayoutParams) pager.getLayoutParams();
                    int offsetx = (int) (positionOffset * (pager.getPageMargin() + margin.rightMargin));
                    pager.setTranslationX(offsetx);
                }
            }

            @Override
            public void onPageSelected(int position) {
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }


    public static class SampleAdapter extends PagerAdapter implements View.OnClickListener {

        private final LayoutInflater inflater;
        private final List<Integer> images;

        public SampleAdapter(Context context, List<Integer> images) {
            this.inflater = LayoutInflater.from(context);
            this.images = images;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            RelativeLayout parallaxContainer = (RelativeLayout) inflater.inflate(R.layout.page_layout, null);
            ImageView image = (ImageView) parallaxContainer.findViewById(R.id.image);
            image.setImageResource(images.get(position));
            container.addView(parallaxContainer);
            parallaxContainer.setTag(position);
            parallaxContainer.setOnClickListener(this);
            return parallaxContainer;
        }

        @Override
        public int getCount() {
            return images.size();
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public boolean isViewFromObject(View view, Object o) {
            return view == o;
        }

        @Override
        public int getItemPosition(Object object) {
            return POSITION_NONE;
        }

        @Override
        public void onClick(View v) {
            Toast.makeText(v.getContext(), v.getTag() + "", Toast.LENGTH_SHORT).show();
        }
    }
}
